﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using ScrumTable.Common.Text;

#endregion

namespace ScrumTable.Common.Test.Text
{
    /// <summary>
    /// This is the test class for IniParser class.
    /// </summary>
    [TestClass]
    public class IniParserTest
    {
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext { get; set; }

        #endregion

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        /// <summary>
        /// Creates a new IniParserTest instance.
        /// </summary>
        public IniParserTest()
        {
        }

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        [TestMethod]
        public void TestParse()
        {
            TestParseString(Resources.IniParseString);
            TestParseString(Resources.IniParseStringOutput);
        }

        [TestMethod]
        public void TestParseWithMultipleValues()
        {
            TestParseMultipleString(Resources.IniParseString);
        }

        [TestMethod]
        public void TestToStringWithMultipleValues()
        {
            TestParseMultipleString(new IniParser().ToString(Resources.IniParseDataMultiple, s => s.First()));
        }

        [TestMethod]
        public void TestToString()
        {
            TestParseString(new IniParser().ToString(Resources.IniParseData));
        }

        private void TestParseString(string input)
        {
            IniParser parser = new IniParser();
            IDictionary<string, string> data = parser.Parse(input);
            TestParseString(data, Assert.AreEqual);
        }

        private void TestParseMultipleString(string input)
        {
            IniParser parser = new IniParser();
            IDictionary<string, ICollection<string>> data = parser.Parse(input, s => new[] { s });
            TestParseString(data, (e, c) => Assert.AreEqual(e, c.First()));
        }

        private void TestParseString<T>(IDictionary<string, T> data, Action<string, T> toCheckValue)
        {
            Assert.AreEqual(Resources.IniParseData.Count, data.Count);

            foreach (KeyValuePair<string, T> dataPair in data)
            {
                Assert.IsTrue(Resources.IniParseData.ContainsKey(dataPair.Key));
                toCheckValue(Resources.IniParseData[dataPair.Key], dataPair.Value);
            }
        }

        #endregion

        #region Events
        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------

        #endregion
    }
}
